import os

from flask import Flask
from flask import jsonify
from flask import request

from .. import agent
from .. import board
from ..utils import CoordsFromPoint, PointFromCoords

__all__ = [
    'get_web_app',
]


def get_web_app(bot_map):
    """Create a flask application for serving bot moves.

    The bot_map maps from URL path fragments to Agent instances.

    The /static path will return some static content (including the
    jgoboard JS).

    Clients can get the post move by POSTing json to
    /select-move/<bot name>

    Example:

    >>> myagent = agent.RandomBot()
    >>> web_app = get_web_app({'random': myagent})
    >>> web_app.run()

    Returns: Flask application instance
    """
    here = os.path.dirname(__file__)
    static_path = os.path.join(here, 'static')
    app = Flask(__name__, static_folder=static_path, static_url_path='/static')

    @app.route('/select-move/<bot_name>', methods=['POST'])
    def select_move(bot_name):
        content = request.json
        board_size = content['board_size']
        game_state = board.GameState.NewGame(board_size)
        # Replay the game up to this point.
        for move in content['moves']:
            if move == 'pass':
                next_move = board.Move.Pass()
            elif move == 'resign':
                next_move = board.Move.Resign()
            else:
                next_move = board.Move.Play(PointFromCoords(move))
            game_state = game_state.ApplyMove(next_move)
        bot_agent: agent.DeepLearningAgent = bot_map[bot_name]
        bot_move: board.Move = bot_agent.SelectMove(game_state)
        if bot_move.is_pass:
            bot_move_str = 'pass'
        elif bot_move.is_resign:
            bot_move_str = 'resign'
        else:
            bot_move_str = CoordsFromPoint(bot_move.point)
        print(bot_move_str)
        return jsonify({
            'bot_move': bot_move_str,
            'diagnostics': bot_agent.Diagnostics()
        })

    return app
